package com.hb.trans.mvp.ui.activity


import android.annotation.SuppressLint
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.basiclib.base.BaseActivity
import com.basiclib.utils.ImmersionBar
import com.hb.trans.R
import com.hb.trans.bean.Period
import com.hb.trans.global.ACTIVITY_TRANS_RECORD
import com.hb.trans.global.ACTIVITY_TRANS_RECORD_EVALUATE
import com.hb.trans.global.DataStore
import com.hb.trans.mvp.contract.TransRecordContract
import com.hb.trans.mvp.presenter.TransRecordPresenter
import com.hb.trans.mvp.ui.adapter.TransRecordAdapter
import com.hb.trans.utils.ConvertUtils
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_trans_record.*
import javax.inject.Inject

@Route(path = ACTIVITY_TRANS_RECORD)
class TransRecordActivity : BaseActivity(), TransRecordContract.View {

    @Inject
    lateinit var mPresenter: TransRecordPresenter
    @Inject
    lateinit var mAdapter: TransRecordAdapter

    @Autowired
    @JvmField
    var part: Int = 1
    @Autowired
    @JvmField
    var validTime: Int = 0
    @Autowired
    @JvmField
    var requireTime: Int = 0

    override fun getContentViewResId(): Int = R.layout.activity_trans_record

    override fun onActivityInject() {
        AndroidInjection.inject(this)
    }

    override fun attachView(savedInstanceState: Bundle?) {
        ARouter.getInstance().inject(this)
        mPresenter.attachView(this)
        ImmersionBar.with(this)
            .statusBarColor(R.color.colorPrimary)
            .init()
    }

    override fun getPresenter(): TransRecordPresenter? = mPresenter

    override fun initView() {
        transRecordToolbar.setTitle(partTransToUpCase(part))
        tvTotalQualiflyTime.text = ConvertUtils.convertToPeriodHour(validTime)
        tvNeedTransTime.text = ConvertUtils.convertToPeriodHour(requireTime - validTime)
        rvTransRecord.apply {
            layoutManager = androidx.recyclerview.widget.LinearLayoutManager(
                this@TransRecordActivity,
                androidx.recyclerview.widget.LinearLayoutManager.VERTICAL,
                false
            )
            adapter = mAdapter
        }
    }

    @SuppressLint("CheckResult")
    override fun setListener() {
        transRecordToolbar.backClick()?.subscribe { finish() }
        mAdapter.setOnEvaluateClickListener(object : TransRecordAdapter.OnEvaluateClickListener {
            override fun onClick(period: Period) {
                if (period.rated == 0) {
                    ARouter.getInstance()
                        .build(ACTIVITY_TRANS_RECORD_EVALUATE)
                        .withObject("period", period)
                        .navigation()
                }
            }

        })
    }

    override fun initData() {

    }

    override fun onResume() {
        super.onResume()
        val student = DataStore.getInstance().getStudentInfo()
        student?.let {
            mPresenter.getPeriodDetail(it.stunum, part)
        }
    }

    override fun setupData(periods: MutableList<Period>?) {
        periods?.let {
            if (it.size > 0) {
                tvTotalQualiflyTime.text = ConvertUtils.convertToPeriodHour(it[0].hgTime)
                tvNeedTransTime.text = ConvertUtils.convertToPeriodHour(it[0].syTime)
            }
            mAdapter.setNewData(periods)
        }

    }

    private fun partTransToUpCase(part: Int) =
        when (part) {
            1 -> "第一部分学时记录"
            2 -> "第二部分学时记录"
            3 -> "第三部分学时记录"
            4 -> "第四部分学时记录"
            else -> ""
        }


    override fun onDestroy() {
        super.onDestroy()
        ImmersionBar.with(this).destroy()
    }
}
